package pkg16b_eca.data.validate;

import java.util.ArrayList;
import java.util.HashMap;

/** Validating arrays and so... */
public class ArrayValidation {
    public ArrayValidation() {}
    
    /** Returning a safe array/hashmap. */
    public static HashMap<String, String> SelectRow(final HashMap<String, String> rows) {
        HashMap<String, String> validatedRows = new HashMap<>();
        
        //If rows aren't null then go through every item and correct them according to the Text.getSpecialCharacters(String).
        if (rows != null) {
            for (String r : rows.keySet()) {
                if (r != null && !r.isEmpty()) {
                    validatedRows.put(r, Text.getSpecialCharacters(rows.get(r)));
                }
            }
        }
        //If it's empty, then use the old one. Because there was no refactoring needed.
        if (validatedRows.isEmpty()) {
            validatedRows = rows;
        }
        return validatedRows;
    }
    
    /** Returning a safe arrays. */
    public static ArrayList<HashMap<String, String>> SelectRows(final ArrayList<HashMap<String, String>> rows) {
        ArrayList<HashMap<String, String>> validatedList = new ArrayList<>();
        HashMap<String, String> tmpRows = new HashMap<>();
        
        //If rows aren't null then go through every item and correct them according to the Text.getSpecialCharacters(String).
        if (rows != null) {
            for (HashMap<String, String> itemRow : rows) {
                for (String r : itemRow.keySet()) {
                    if (r != null && !r.isEmpty()) {
                        tmpRows.put(r, Text.getSpecialCharacters(itemRow.get(r)));
                    }
                }
                //New hash map when adding to a list, because if the hashmap is cleared after the adding, the added value will also get cleared because it still has a reference.
                validatedList.add(new HashMap<>(tmpRows));
                tmpRows.clear();
            }
        }
        //If it's empty, then use the old one. Because there was no refactoring needed.
        if (validatedList.isEmpty()) {
            validatedList = rows;
        }
        return validatedList;
    }
    
    /** Returning a safe matrix array. */
    public static ArrayList<ArrayList<String>> SelectMatrix(final ArrayList<ArrayList<String>> list) {
        ArrayList<ArrayList<String>> validatedList = new ArrayList<>();
        ArrayList<String> tmpList = new ArrayList<>();
        
        //If rows aren't null then go through every item and correct them according to the Text.getSpecialCharacters(String).
        if (list != null) {
            for (ArrayList<String> item : list) {
                for (String text : item) {
                    if (text != null && !text.isEmpty()) {
                        tmpList.add(Text.getSpecialCharacters(text));
                    }
                }
                //New array list when adding a value to a list and then clearing it after, because if it's cleared after added to the validatedList, the one in validatedList will also get cleared because it still has a reference.
                validatedList.add(new ArrayList<>(tmpList));
                tmpList.clear();
            }
        }
        //If it's empty, then use the old one. Because there was no refactoring needed.
        if (validatedList.isEmpty()) {
            validatedList = list;
        }
        return validatedList;
    }
}
